package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.Oppo;
import com.manger.model.Project;
import com.manger.utils.CopyFiles;
import com.manger.utils.DetNameEnum;
import com.manger.utils.LoanFileEnum;
import com.manger.utils.SystemConstant;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName OppoCaseFilesFormat
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/9/1 16:00
 * @Version 1.0
 */
public class OppoCaseFilesFormat implements CaseFilesFormat {

    //D:\BaiduNetdiskDownload\OPPO
    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        String caseFilesPath = extractDir + "/" + project.getDebtName();
        List<String> ignoreFilesList = project.getIgnoreFilesList();
        File file = new File(caseFilesPath);
        if (!file.exists()) {
            System.out.println("没有查询到材料信息！【" + caseFilesPath + "】");
            return;
        }
        String excelPath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        Map<String, List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(new File(excelPath), Oppo.class).get();
        if (map.keySet().size() <= 0) {
            System.out.println("没有解析到委托信息！");
            return;
        }
        List<Oppo> oppoList = (List<Oppo>) map.get(map.keySet().iterator().next());
        Map<String, String> caseFilePathMap = new HashMap<>();
        File[] dirCaseFiles = file.listFiles();
        for (File dirCaseFile : dirCaseFiles) {
            if(!dirCaseFile.isDirectory()) {
                continue;
            }
            File[] fileCaseFiles = dirCaseFile.listFiles();
            boolean flag = false;
            for(File fileCase : fileCaseFiles) {
                String fileName = fileCase.getName();
                String outOrderId = fileName.substring(fileName.lastIndexOf("-") + 1, fileName.length());
                caseFilePathMap.put(outOrderId, fileCase.getCanonicalPath());
            }
        }

        String mPath = extractDir + "/" + "材料";
        File mFile = new File(mPath);
        Map<String,String> mCaseFilePathMap = new HashMap<>();
        for (Oppo oppo : oppoList) {
            String dirCaseFilePath = caseFilePathMap.get(oppo.getOutOrderId());
            if (StringUtils.isEmpty(dirCaseFilePath)) {
                continue;
            }
            String targetPath = targetDir + "/" + project.getDebtName() + "/"
                    + oppo.getPlaintiffName() + "/" + SystemConstant.CASE_FILE_DIR_NAME + "/" + oppo.getUserName() + "-" + oppo.getIdCard();
            File temp = new File(targetPath);
            if (!temp.exists()) {
                temp.mkdirs();
                CopyFiles.createDirs(targetPath);
            }
            File resourceDir = new File(dirCaseFilePath);
            File[] resourceFiles = resourceDir.listFiles();
            for(File resourceFile : resourceFiles) {
                boolean flag = false;
                String fileName = resourceFile.getName();
                for(String ignoreFileName: ignoreFilesList){
                    if(fileName.contains(ignoreFileName)){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    continue;
                }
                CopyFiles.copyFileToDir(resourceFile.getCanonicalPath(),targetPath + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
            }
            System.out.println("整理数据：【"+oppo.getPlaintiffName()+ " " + oppo.getUserName() + "-" + oppo.getIdCard() +"】");
            mCaseFilePathMap.put(oppo.getSueID(), targetPath);
        }
        System.out.println("拷贝材料");
        //拷贝其他材料
        File[] mDirs = mFile.listFiles();
        for(File mDir : mDirs) {
            String dirName = mDir.getName();
            File[] dirFiles = mDir.listFiles();
            for(File dirFile : dirFiles) {
                String fileName = dirFile.getName();
                for(String key: mCaseFilePathMap.keySet()) {
                    if(fileName.startsWith(key)){
                        String targetPath = mCaseFilePathMap.get(key) + "/" + dirName;
                        CopyFiles.copyFileToDir(dirFile.getCanonicalPath(),targetPath);
                    }
                }
            }
        }
        System.out.println("拷贝通用配置材料");
        String path = targetDir + "/" + project.getDebtName() + "/";
        File caseDir = new File(path);
        File[] caseFiles = caseDir.listFiles();
        for(File caseFile : caseFiles) {
            String dirName = caseFile.getName();
            String targetDescPath = caseFile.getCanonicalPath() + "/" + SystemConstant.CASE_FILE_DIR_NAME + "/";
            File descFile = new File(targetDescPath);
            File[] descFiles = descFile.listFiles();
            for(File dirDescFile : descFiles) {
                String configPath = SystemConstant.getConfigDir() + "/" + project.getDebtName() + "/" + dirName;
                File configFile = new File(configPath);
                if(!configFile.exists()){
                    continue;
                }
                File[] configFiles = configFile.listFiles();
                for(File caseDescFile : configFiles) {
                    CopyFiles.copyDirToDir(caseDescFile.getCanonicalPath(),dirDescFile.getCanonicalPath() + "/" + caseDescFile.getName());
                }
            }
        }
    }
    public static String findStrByRegex(String content, String regex){
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group();
        }
        return "";
    }
    public static void main(String[] args) throws Exception {
       /* String fileName = "隆携小贷-分期-FCLNiCPiY8393303040MTSOT1ZQM2X";
        System.out.println(fileName.substring(fileName.lastIndexOf("-") + 1, fileName.length()));*/

        OppoCaseFilesFormat oppoCaseFilesFormat = new OppoCaseFilesFormat();
        String targetDir = "C:\\Users\\liuhj\\Desktop\\案件整理结果";
        String extractDir = "C:\\Users\\liuhj\\Desktop\\oppo";
        Project project = new Project();
        project.setDebtName(DetNameEnum.OPPO.getDesc());
        oppoCaseFilesFormat.format(extractDir,targetDir,project);
      /*  String name = "隆携810271813347057665左维旅要素式民事起诉状_631";
        String idCard = OppoCaseFilesFormat.findStrByRegex(name,"\\d{17}[0-9Xx]");
        System.out.println(name.substring(0,name.indexOf(idCard)+17));*/
      //  System.out.println(name.startsWith("隆携810271813347057665"));
    }
}
